package com.rocket.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.rocket.R;
import com.rocket.database.WorkStationSql;
import com.rocket.entity.Position;
import com.rocket.entity.WorkStation;
import com.rocket.util.DialogUtil;
import com.rocket.util.MenuUtil;
import com.rocket.util.MyTextWatcher;

public class WorkStationAddActivity extends MenuActivity {

	Button buttonBack;
	Button buttonSave;

	LinearLayout content;

	EditText etName;
	EditText etNumber;
	EditText etFixedLon;
	EditText etFixedLat;
	EditText etHaiba;

	Button buttonClear;

	Button buttonAdd;

	ArrayList<LinearLayout> lines;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.workstationadd);

		buttonBack = (Button) findViewById(R.id.buttonback);
		buttonBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		buttonSave = (Button) findViewById(R.id.buttonsave);
		buttonSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (etName.getText().toString().trim().equals("")) {
					showToast("请输入作业点名");
					etName.requestFocus();
					return;
				}
				if (etNumber.getText().toString().trim().equals("")) {
					showToast("请输入作业点编号");
					etNumber.requestFocus();
					return;
				}
				if (etFixedLon.getText().toString().trim().equals("")) {
					showToast("请输入固定作业点经度");
					etFixedLon.requestFocus();
					return;
				}
				if (etFixedLat.getText().toString().trim().equals("")) {
					showToast("请输入固定作业点纬度");
					etFixedLat.requestFocus();
					return;
				}
				if (etHaiba.getText().toString().trim().equals("")) {
					showToast("请输入固定作业点纬度");
					etHaiba.requestFocus();
					return;
				}
				if (WorkStationSql.getInstance().isNameExists(
						etName.getText().toString().trim())) {
					showToast("作业站点名已存在");
					etName.requestFocus();
					return;
				}
				if (WorkStationSql.getInstance().isNumberExists(
						etNumber.getText().toString().trim())) {
					showToast("作业站点编号已存在");
					etNumber.requestFocus();
					return;
				}
				WorkStation station = new WorkStation();
				station.name = etName.getText().toString().trim();
				station.number = etNumber.getText().toString().trim();
				station.haiba = Integer.valueOf(etHaiba.getText().toString()
						.trim());
				station.positions = new ArrayList<Position>();
				station.positions.add(new Position(etFixedLon
						.getText().toString().trim(),etFixedLat
						.getText().toString().trim()));
				if (lines != null) {
					for (LinearLayout line : lines) {
						EditText etlon = (EditText) line
								.findViewById(R.id.etlon);
						EditText etlat = (EditText) line
								.findViewById(R.id.etlat);
						if (etlon.getText().toString().trim().equals("")
								|| etlat.getText().toString().trim().equals("")) {
							continue;
						}
						String lon = etlon.getText().toString()
								.trim();
						String lat = etlat.getText().toString()
								.trim();
						Position position = new Position(lon, lat);
						station.positions.add(position);
					}
				}
				WorkStationSql.getInstance().insert(station);
				showToast("站点添加成功！");
				etName.setText("");
				etNumber.setText("");
				etHaiba.setText("");
				etFixedLat.setText("");
				etFixedLon.setText("");
				if (lines != null) {
					for (LinearLayout line : lines) {
						content.removeView(line);
					}
					lines.clear();
				}
			}
		});

		content = (LinearLayout) findViewById(R.id.content);

		etName = (EditText) findViewById(R.id.etname);
		etNumber = (EditText) findViewById(R.id.etnumber);
		etFixedLon = (EditText) findViewById(R.id.etfixedlon);
		etFixedLat = (EditText) findViewById(R.id.etfixedlat);
		etHaiba = (EditText) findViewById(R.id.ethaiba);
		etHaiba.addTextChangedListener(new MyTextWatcher(etHaiba));

		buttonClear = (Button) findViewById(R.id.btnclear);
		buttonClear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				etFixedLon.setText("");
				etFixedLat.setText("");
			}
		});

		buttonAdd = (Button) findViewById(R.id.btnadd);
		buttonAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final LinearLayout line = (LinearLayout) getLayoutInflater()
						.inflate(R.layout.fixedworkpoint, null);

				Button btnclear = (Button) line.findViewById(R.id.btnclear);
				btnclear.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						content.removeView(line);
						lines.remove(line);
					}
				});
				content.addView(line);
				if (lines == null) {
					lines = new ArrayList<LinearLayout>();
				}
				lines.add(line);
			}
		});
	}

	@Override
	protected void openMenu() {
		String[] texts = new String[] { "火器选择", "历史查询", "退出程序" };
		Integer[] images = new Integer[] { R.drawable.menu_logout,
				R.drawable.menu_sys_search_buddy, R.drawable.menu_icon };
		mMenu = new MenuUtil(WorkStationAddActivity.this, getLayoutInflater()
				.inflate(R.layout.workstationadd, null), texts, images);
		mMenu.setMenuItemSelectedLinstener(new OnClickListener() {

			public void onClick(View v) {
				int tag = (Integer) v.getTag();
				switch (tag) {
				case 0:
					mMenu.dismiss();
					Intent intent1 = new Intent(WorkStationAddActivity.this,
							ChoseFirearmActivity.class);
					intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent1);
					break;
				case 1:
					mMenu.dismiss();
					Intent intent3 = new Intent(WorkStationAddActivity.this,
							HistoryQueryActivity.class);
					intent3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent3);
					break;
				case 2:
					mMenu.dismiss();
					mDialog = new DialogUtil(WorkStationAddActivity.this);
					mDialog.setTitle("退出确认");
					mDialog.setMessage("确定退出吗？");
					mDialog.setPositiveButton("确定", new OnClickListener() {

						public void onClick(View v) {
							mDialog.dismiss();
							Intent intent = new Intent(
									WorkStationAddActivity.this,
									MainActivity.class);
							intent.putExtra("Flag", 1);
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							startActivity(intent);
						}
					});
					mDialog.setNegativeButton("取消", null);
					mDialog.show();
					break;
				default:
					break;
				}
			}
		});
		mMenu.show();
	}

}
